<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
class Guestbook extends Controller
{
	function Guestbook()
	{
		parent::Controller();
		check_is_login();
		$this->load->model("admin/Guestbook_model");
		$this->load->library('form_validation');
		$this->load->database();
	}
	function index()
	{
		$per_page = 10;
		$query = $this->Guestbook_model->get_records($per_page);
		$data = array(
				'query'		=> $query['query'],
				'paginate'	=> $query['paginate'],
				'is_pass_num'  => $this->Guestbook_model->get_is_pass_num()
		);
		$this->load->view(TPL_FOLDER."guestbook_list",$data);
	}
	
	function edit_record()
	{
		$this->load->model("Fck_model");
		$rd_id=$this->uri->segment(4,0);
		parse_str($_SERVER['QUERY_STRING'],$get);
		if(!$rd_id) 
		{
			$tx_msg="<li>ID参数有误.</li>";
			echo_msg($tx_msg);
		}
		$edit_data=$this->Guestbook_model->get_record($rd_id);
		$data=array(
			'reply'=> $this->Fck_model->fckeditor("reply",$edit_data['reply']),
			'edit_data'=>$edit_data,
			'url'=>$get['url']
		);
		$this->load->view(TPL_FOLDER."guestbook_edit",$data);
	}
	
	function save_record()
	{
		$this->form_validation->set_rules("rd_id","记录ID","trim|integer");
		$tx_msg="";
		if($this->form_validation->run()==FALSE)
		{
			$tx_msg.=validation_errors();
		}
		if($tx_msg!="")
		{
			echo_msg($tx_msg);
		}
		if($this->Guestbook_model->save_record())
		{
			
			$tx_msg="<li>记录修改成功.</li>";
			echo_msg($tx_msg,$this->input->post('url'),'yes');
		}
		else
		{
			$tx_msg="<li>记录修改失败.</li>";
			echo_msg($tx_msg);
		}
	}

	function del_record()
	{
		$this->form_validation->set_rules("rd_id","删除的记录","required");
		if($this->form_validation->run()==FALSE)
		{
			$tx_msg=validation_errors();
			echo_msg($tx_msg);
		}
		if($this->Guestbook_model->del_record())
		{
			
			$tx_msg="<li>记录删除成功.</li>";
			echo_msg($tx_msg,'','yes');
		}
		else
		{
			$tx_msg="<li>记录删除失败.</li>";
			echo_msg($tx_msg);
		}
	}
	
	function pass()
	{
		$this->form_validation->set_rules("rd_id","移动的项","required");
		if($this->form_validation->run()==FALSE)
		{
			$tx_msg=validation_errors();
			echo_msg($tx_msg);
		}
		if($this->Guestbook_model->pass())
		{
			
			$tx_msg="<li>记录审核成功.</li>";
			echo_msg($tx_msg,'','yes');
		}
		else
		{
			$tx_msg="<li>记录审核失败.</li>";
			echo_msg($tx_msg);
		}
	}
	
	function ajax_toggle()
	{
		$this->form_validation->set_rules("rd_id","0","trim|required");
		if($this->form_validation->run()==FALSE)
		{
			die("0");
		}
		if($this->Guestbook_model->ajax_toggle())
		{
			
			echo "1";
		}
		else
		{
			echo "0";
		}
	}

}
?>